package org.dromara.nursing.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
// import cn.dev33.satoken.annotation.SaCheckPermission; // Ensure this is commented
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.nursing.domain.vo.RoomInfoVo;
import org.dromara.nursing.domain.bo.RoomInfoBo;
import org.dromara.nursing.service.IRoomInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 房间管理
 *
 * @author Xue
 * @date 2025-04-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/nursing/roomInfo")
public class RoomInfoController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(RoomInfoController.class);

    private final IRoomInfoService roomInfoService;

    /**
     * 查询房间管理列表
     */
    // @SaCheckPermission("nursing:roomInfo:list") // 已移除权限验证
    @GetMapping("/list")
    public TableDataInfo<RoomInfoVo> list(RoomInfoBo bo, PageQuery pageQuery) {
        return roomInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出房间管理列表
     */
    // @SaCheckPermission("nursing:roomInfo:export") // 已移除权限验证
    @Log(title = "房间管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(RoomInfoBo bo, HttpServletResponse response) {
        List<RoomInfoVo> list = roomInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "房间管理", RoomInfoVo.class, response);
    }

    /**
     * 获取房间管理详细信息
     *
     * @param roomId 主键
     */
    // @SaCheckPermission("nursing:roomInfo:query") // 已移除权限验证
    @GetMapping("/{roomId}")
    public R<RoomInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long roomId) {
        return R.ok(roomInfoService.queryById(roomId));
    }

    /**
     * 新增房间管理
     */
    // @SaCheckPermission("nursing:roomInfo:add") // 已移除权限验证
    @Log(title = "房间管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody RoomInfoBo bo) {
        try {
            boolean result = roomInfoService.insertByBo(bo);
            if (result) {
                return R.ok();
            } else {
                return R.fail("新增房间失败");
            }
        } catch (Exception e) {
            log.error("新增房间异常", e);
            return R.fail("新增房间异常: " + e.getMessage());
        }
    }

    /**
     * 修改房间管理
     */
    // @SaCheckPermission("nursing:roomInfo:edit") // 已移除权限验证
    @Log(title = "房间管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody RoomInfoBo bo) {
        return toAjax(roomInfoService.updateByBo(bo));
    }

    /**
     * 删除房间管理
     *
     * @param roomIds 主键串
     */
    // @SaCheckPermission("nursing:roomInfo:remove") // 已移除权限验证
    @Log(title = "房间管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roomIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] roomIds) {
        try {
            log.info("删除房间请求，roomIds: {}", (Object) roomIds);
            boolean result = roomInfoService.deleteWithValidByIds(List.of(roomIds), true);
            if (result) {
                return R.ok();
            } else {
                return R.fail("删除房间失败，可能房间已被使用或不存在");
            }
        } catch (Exception e) {
            log.error("删除房间异常", e);
            return R.fail("删除房间异常: " + e.getMessage());
        }
    }
}
